//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm;
using EntLibContrib.Resource;
using EntLibContrib.Resource.Configuration;

namespace EntLibContrib.Resource.Configuration.Manageability
{
	/// <summary>
	/// Manageability provider for the CustomResourceProviderData type
	/// </summary>
	internal class CustomResourceProviderDataManageabilityProvider
		: CustomProviderDataManageabilityProvider<CustomResourceProviderData>
	{
		#region Constants
		/// <summary>
		/// Provider type property name
		/// </summary>
		public new const String ProviderTypePropertyName = CustomProviderDataManageabilityProvider<CustomResourceProviderData>.ProviderTypePropertyName;
		/// <summary>
		/// Attributes property name
		/// </summary>
		public new const String AttributesPropertyName = CustomProviderDataManageabilityProvider<CustomResourceProviderData>.AttributesPropertyName;
		/// <summary>
		/// Resource base name property name
		/// </summary>
		public const String ResourceBaseNamePropertyName = "resourceBaseName";
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="CustomResourceProviderDataManageabilityProvider"/> class.
		/// </summary>
		public CustomResourceProviderDataManageabilityProvider()
			: base("")
		{ }
		#endregion

		#region Protected Methods
		/// <summary>
		/// Adds the ADM instructions that describe the policies that can be used to override the properties of
		/// a specific instance of the configuration element type managed by the receiver.
		/// </summary>
		/// <param name="contentBuilder">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder"/> to which the Adm instructions are to be appended.</param>
		/// <param name="configurationObject">The configuration object instance.</param>
		/// <param name="configurationSource">The configuration source from where to get additional configuration
		/// information, if necessary.</param>
		/// <param name="elementPolicyKeyName">The key for the element's policies.</param>
		/// <remarks>
		/// The default implementation for this method creates a policy, using
		/// <see cref="P:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1.ElementPolicyNameTemplate"/> to create the policy name and invoking
		/// <see cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1.AddElementAdministrativeTemplateParts(Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder,`0,Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource,System.String)"/>
		/// to add the policy parts.
		/// Subclasses managing objects that must not create a policy must override this method to just add the parts.
		/// </remarks>
		protected override void AddAdministrativeTemplateDirectives(AdmContentBuilder contentBuilder,
			CustomResourceProviderData configurationObject,
			IConfigurationSource configurationSource,
			String elementPolicyKeyName)
		{
			// parts for providers are part of their resource manager's policies
			AddElementAdministrativeTemplateParts(contentBuilder,
				configurationObject,
				configurationSource,
				elementPolicyKeyName);
		}

		/// <summary>
		/// Adds the ADM parts that represent the properties of
		/// a specific instance of the configuration element type managed by the receiver.
		/// </summary>
		/// <param name="contentBuilder">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder"/> to which the Adm instructions are to be appended.</param>
		/// <param name="configurationObject">The configuration object instance.</param>
		/// <param name="configurationSource">The configuration source from where to get additional configuration
		/// information, if necessary.</param>
		/// <param name="elementPolicyKeyName">The key for the element's policies.</param>
		/// <remarks>
		/// Subclasses that manage custom provider's configuration objects with additional properties may
		/// override this method to add the corresponding parts.
		/// </remarks>
		/// <seealso cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1.AddAdministrativeTemplateDirectives(Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder,`0,Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource,System.String)"/>
		protected override void AddElementAdministrativeTemplateParts(AdmContentBuilder contentBuilder,
			CustomResourceProviderData configurationObject,
			IConfigurationSource configurationSource,
			String elementPolicyKeyName)
		{
			contentBuilder.AddEditTextPart(InternalResources.CustomResourceProviderDataResourceBaseNamePartName,
				elementPolicyKeyName,
				ResourceBaseNamePropertyName,
				configurationObject.ResourceBaseName,
				255,
				true);

			contentBuilder.AddEditTextPart(InternalResources.CustomResourceProviderDataTypePartName,
				elementPolicyKeyName,
				ProviderTypePropertyName,
				configurationObject.Type.AssemblyQualifiedName,
				1024,
				true);

			contentBuilder.AddEditTextPart(InternalResources.CustomResourceProviderDataAttributesPartName,
				elementPolicyKeyName,
				AttributesPropertyName,
				GenerateAttributesString(configurationObject.Attributes),
				1024,
				false);
		}

		/// <summary>
		/// Overrides the with group policies.
		/// </summary>
		/// <param name="configurationObject">The configuration object.</param>
		/// <param name="policyKey">The policy key.</param>
		/// <remarks>Subclasses that manage custom provider's configuration objects with additional properties may
		/// override this method to override these properties.</remarks>
		protected override void OverrideWithGroupPolicies(CustomResourceProviderData configurationObject, IRegistryKey policyKey)
		{
			String resourceBaseNameOverride = policyKey.GetStringValue(ResourceBaseNamePropertyName);

			configurationObject.ResourceBaseName = resourceBaseNameOverride;
		}

		/// <summary>
		/// Creates the setting.
		/// </summary>
		/// <param name="data">The data.</param>
		/// <param name="attributes">The attributes.</param>
		/// <returns></returns>
		protected override NamedConfigurationSetting CreateSetting(CustomResourceProviderData data, String[] attributes)
		{
			return new CustomResourceProviderSetting(data.Name, data.ResourceBaseName, data.Type.AssemblyQualifiedName, attributes);
		}
		#endregion
	}
}
